package chan.project.smartlawregularuser.controller;

import chan.project.smartlawcommon.common.BaseResponse;
import chan.project.smartlawcommon.common.ErrorCode;
import chan.project.smartlawcommon.common.ResultUtils;
import chan.project.smartlawcommon.exception.BusinessException;
import chan.project.smartlawcommon.exception.ThrowUtils;
import chan.project.smartlawcommon.utils.FileContentExtractorUtil;
import chan.project.smartlawmodel.dto.ai.TextParseRequest;
import chan.project.smartlawmodel.dto.data.LegalCase;
import chan.project.smartlawmodel.entity.LawyerInfo;
import chan.project.smartlawmodel.enums.TextParseEnum;
import chan.project.smartlawmodel.vo.LawyerInfoVO;
import chan.project.smartlawmodel.vo.LegalCaseVo;
import chan.project.smartlawservice.AiService.AiFeignClient;
import chan.project.smartlawservice.lawyerService.LawyerInfoFeignClient;
import cn.hutool.core.collection.CollectionUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户功能接口，用于实现用户所能调用的功能服务
 */
@Tag(name = "普通用户功能接口")
@RestController
@RequestMapping("/common")
@Slf4j
public class UserFunctionController {

    @Resource
    private LawyerInfoFeignClient lawyerInfoFeignClient;

    @Resource
    private AiFeignClient aiFeignClient;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 文件审查
     * @param file
     * @param userRequire
     * @param type
     */
    @Operation(summary = "documentReview")
    @PostMapping(path = "/review/document",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE
            ,produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> documentReview(@RequestParam("file") MultipartFile file,
                                             @RequestParam("userRequire")String userRequire,
                                             @RequestParam("type")String type) {
        ThrowUtils.throwIf(file.isEmpty(), ErrorCode.PARAMS_ERROR,"文件为空");
        TextParseEnum enumByValue = TextParseEnum.getEnumByValue(type);
        if(enumByValue == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"文件类型错误");
        }
        String fileContent;
        try {
            fileContent = FileContentExtractorUtil.extractText(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 这里可以添加你对文件内容的处理逻辑
        TextParseRequest textParseRequest = new TextParseRequest();
        textParseRequest.setText(fileContent);
        textParseRequest.setUserRequire(userRequire);
        textParseRequest.setTextParseEnum(enumByValue);
        Flux<String> stringFlux = aiFeignClient.parseText(textParseRequest);
        return stringFlux;
    }

    /**
     * 文本审查
     */
    @Operation(summary = "textReview")
    @PostMapping("/review/text")
    public void textReview() {
        return;
    }


    /**
     * 模拟诉讼
     * @param legalCase
     * @return
     */
    @PostMapping("/lawsuit")
    private BaseResponse<LegalCaseVo> litigationSimulator(@RequestBody LegalCase legalCase) {
        LegalCaseVo legalCaseVo = aiFeignClient.litigationSimulator(legalCase);
        return ResultUtils.success(legalCaseVo);
    }

    /**
     * 获取所有的在线律师信息
     * @return
     */
    @GetMapping("getOnlineLawyer")
    private BaseResponse<List<LawyerInfoVO>> getOnlineLawyer(){
        Set<Object> onlineUserList = redisTemplate.opsForSet().members("onlineUserList");
        List<String> collect = onlineUserList.stream().map(userId -> (String) userId).collect(Collectors.toList());
        List<LawyerInfo> lawyerByList = lawyerInfoFeignClient.getLawyerByList(collect);
        if(CollectionUtil.isEmpty(lawyerByList)){
            return ResultUtils.success(new ArrayList<>());
        }
        List<LawyerInfoVO> lawyerInfoVOList = lawyerByList.stream().map(LawyerInfoVO::objToVo).collect(Collectors.toList());
        return ResultUtils.success(lawyerInfoVOList);
    }
}
